package com.shangcheng.server.controller;

import cn.hutool.core.date.DateTime;
import com.shangcheng.model.domain.UmsMenu;
import com.shangcheng.model.domain.UmsResource;
import com.shangcheng.model.domain.UmsRole;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/14 14:12
 */
@RestController
@RequestMapping("/role")
public class UmsRoleController {
    @Autowired
    private UmsRoleService roleService;

    /**
     * 给角色分配菜单
     * @param menuIds
     * @param roleId
     * @return
     */
    @PostMapping("/allocMenu")
    public ResponseEntity allocMenu(Integer[] menuIds,Integer roleId){
        roleService.allocMenu(menuIds,roleId);
        return ResponseEntity.ok(new Result(200,null,"分配成功"));
    }

    /**
     * 添加角色
     * @param umsRole
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity createRole(@RequestBody UmsRole umsRole){
        umsRole.setCreateTime(new DateTime());
        roleService.createRole(umsRole);
        return ResponseEntity.ok(new Result(200,null,"添加成功"));
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity deleteRole(@RequestParam Integer[] ids){
        roleService.deleteRole(ids);
        return ResponseEntity.ok(new Result(200,null,"删除成功"));
    }

    /**
     * 根据名称查询角色列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
   @GetMapping("/list")
    public ResponseEntity findRoleByName(@RequestParam(required = false) String keyword,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult result= roleService.findRoleByName(keyword,pageNum,pageSize);
        return ResponseEntity.ok(new Result(200,result,null));
    }

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("/listAll")
    public ResponseEntity findAllRole(){
       List<UmsRole> list= roleService.findAllRole();
       return ResponseEntity.ok(new Result(200,list,null));
    }

    /**
     * 修改角色
     * @param id
     * @param role
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateRole(@PathVariable("id") Integer id,@RequestBody UmsRole role){
        role.setId(id.longValue());
        roleService.updateRole(role);
        return ResponseEntity.ok(new Result(200,null,"修改成功"));
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public ResponseEntity updateRoleStatus(@PathVariable("id") Integer id,@RequestParam Integer status){
        UmsRole role= new UmsRole();
        role.setId(id.longValue());
        role.setStatus(status);
        roleService.updateRoleStatus(role);
        return ResponseEntity.ok(new Result(200,null,"修改成功"));
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public ResponseEntity listMenu(@PathVariable("roleId") Integer roleId){
        List<UmsMenu> list= roleService.listMenu(roleId);
        return ResponseEntity.ok(new Result(200,list,null));
    }
    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public ResponseEntity listResource(@PathVariable("roleId") Integer roleId){
        List<UmsResource> list= roleService.listResource(roleId);
        return ResponseEntity.ok(new Result(200,list,"null"));
    }
}
